package by.alex.dzeshko.facebooksuperclient.database;

import org.json.JSONObject;

import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.models.FbNewsFeed;
import by.alex.dzeshko.facebooksuperclient.providers.contract.FbContract;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbNewsfeedHelper extends DbManager{

	public DbNewsfeedHelper(Context context, CursorFactory factory) {
		super(context, factory);
	}

	@Override
	public Cursor getItems(String marker) {
		SQLiteDatabase db = getReadableDatabase();
		String selection = FbContract.FeedColumns.TABLE_TYPE+" = ?";
		String[] selectionArgs = new String[]{marker};
		Cursor cursor = db.query(DbManager.FEED_TABLE, null, selection, selectionArgs, null,
				null, FbContract.FeedColumns.CREATED_TIME+" DESC");
		return cursor;
	}

	@Override
	public long addItem(JSONObject jsonObject) {
		SQLiteDatabase db = getWritableDatabase();
		long value;
		ContentValues values = getContentValues(jsonObject);
		value = db.insertWithOnConflict(FEED_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
		return value; 
	}

	@Override
	protected ContentValues getContentValues(JSONObject pItem) {
		ContentValues values = new ContentValues();
		FbNewsFeed feed = new FbNewsFeed(pItem);
		String id = feed.getFromId();
		values.put(FEED_COLUMNS[ID_COL_INDEX], feed.getId());
		values.put(FEED_COLUMNS[TYPE_COL_INDEX], feed.getType());					
		values.put(FEED_COLUMNS[FROM_NAME_COL_INDEX], feed.getFromName());
		values.put(FEED_COLUMNS[FROM_ID_COL_INDEX], id);
		values.put(FEED_COLUMNS[CREATED_TIME_COL_INDEX],feed.getCreatedTime());
		values.put(FEED_COLUMNS[MESSAGE_COL_INDEX], feed.getMessage());
		int count = feed.getLikesCount();
		if(count == -1) count = 0;
		values.put(FEED_COLUMNS[LIKES_COUNT_COL_INDEX], count);
		count = feed.getCommentsCount();
		if(count == -1) count = 0;
		values.put(FEED_COLUMNS[COMMENTS_COUNT_COL_INDEX], count);
		values.put(FEED_COLUMNS[STORY_COL_INDEX], feed.getStory());
		values.put(FEED_COLUMNS[CAPTION_COL_INDEX], feed.getCaption());
		String url = feed.getPicture();
		values.put(FEED_COLUMNS[CONTENT_PICTURE_COL_INDEX], url);
		values.put(FEED_COLUMNS[COMMENT_LINK_COL_INDEX], feed.getCommentLink());
		values.put(FEED_COLUMNS[LIKE_LINK_COL_INDEX], feed.getLikeLink());
		values.put(FEED_COLUMNS[FEED_USER_PICTURE_URL_COL_INDEX], ApiRequest.getProfilePicture(id, ApiRequest.SQUARE));
		String type = feed.getString(FbContract.FeedColumns.TABLE_TYPE);
		values.put(FEED_COLUMNS[TABLE_TYPE_COL_INDEX], type);
		return values;
	}

	public long deleteItem(String itemId) {

		SQLiteDatabase db = getWritableDatabase();
		return db.delete(FEED_TABLE, "_id = " + itemId, null);
	}
	
	public long deleteItem(String where, String itemId) {
		SQLiteDatabase db = getWritableDatabase();
		long value;
		value = db.delete(FEED_TABLE, where+" = ?", new String[]{itemId});
		return value;
	}

}
